package com.bds.btcdc.web.service;

import org.nutz.dao.Sqls;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: kevin
 * @date: 2018/5/16
 * @description:
 */
@Service
public class OrderService {

    @Resource
    NutDao dao;

    /**
     * 获取一个订单id
     * @param prefix XH、SD、ZD
     * @return
     */
    public String getOrderNo(String prefix){
        SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
        String order_id=prefix+df.format(new Date())+getIdAddOne();
        return order_id;
    }

    private long getIdAddOne(){
        Sql select= Sqls.create("SELECT MAX(id) FROM t_order_record");
        select.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                while (rs.next()){
                    long id=rs.getLong(1);
                    return id;
                }
                return null;
            }
        });
        dao.execute(select);
        long id = select.getLong()+1;
        return id;
    }
}
